package com.project.junM.controller;

import com.alibaba.fastjson.JSON;
import com.project.junM.entity.TMember;
import com.project.junM.service.TMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/member")
public class MemberController {

    @Autowired
    private TMemberService tMemberService;

    //登录
    @GetMapping("queryByPhoneNumberPassWord")
    public TMember queryByPhoneNumberPassWord(String phoneNumber, String passWord, HttpSession session){
//        System.out.println(phoneNumber);
//        System.out.println(passWord);
        return tMemberService.queryByPhoneNumberPassWord(phoneNumber,passWord,session);
    }

    //检查用户是否存在
    @GetMapping("queryByPhoneNumber")
    public boolean queryByPhoneNumber(String phoneNumber){
        return tMemberService.queryByPhoneNumber(phoneNumber);
    }

    //检查用户身份证唯一性
    @GetMapping("queryByIdCard")
    public boolean queryByIdCard(String idCard){
//        System.out.println(idCard);
        return tMemberService.queryByIdCard(idCard);
    }

    //注册用户
    @PostMapping("addMember")
    public boolean addMember(@RequestBody Map<String, TMember> map) {
//        System.out.println(1);
//        TMember tMember = JSON.parseObject(JSON.toJSONString(map.get("tMember")), TMember.class);
        return tMemberService.insertSelective(map.get("tMember"));
    }

    //用户预约
    @PostMapping("updateByPrimaryKeySelective")
    public boolean updateByPrimaryKeySelective(@RequestBody Map<String,TMember> map,HttpSession session) {
        TMember tMember = JSON.parseObject(JSON.toJSONString(map.get("tMember")), TMember.class);
        return tMemberService.updateByPrimaryKeySelective(tMember,session);
    }

    //查看个人基本信息
    @GetMapping("queryByMemerId")
    public TMember queryByMemerId(HttpSession session){
        return tMemberService.selectByPrimaryKey(session);
    }

    @GetMapping("queryReprotByMemberId")
    public TMember queryReprotByMemberId(HttpSession session){
        return tMemberService.queryReprotByMemberId(session);
    }

    @GetMapping("sessionFlag")
    public boolean sessionFlag(HttpSession session){
//        TMember user = (TMember)session.getAttribute("user");
        session.removeAttribute("user");
        return true;
    }

    @GetMapping("queryMemberAlls")
    public TMember queryMemberAlls(HttpSession session){
        return this.tMemberService.queryMemberAlls(session);
    }

}
